import logging
import time
import os
import colorlog



# log_colors_config = {
#     'DEBUG': 'cyan',
#     'INFO': 'green',
#     'WARNING': 'yellow',
#     'ERROR': 'red',
#     'CRITICAL': 'red',
# }
from configuration.log_config import LogConfig


def log_creater(output_dir: str):
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    log_name = '{}.log'.format(time.strftime('%Y-%m-%d-%H-%M'))
    final_log_file = os.path.join(output_dir, log_name)
    # creat a logger
    logger = logging.getLogger(LogConfig.LOGGER_NAME)
    logger.setLevel(logging.DEBUG)

    # FileHandler
    file = logging.FileHandler(final_log_file)
    file.setLevel(LogConfig.FILE_LOG_LEVEL)

    # StreamHandler
    stream = colorlog.StreamHandler()
    stream.setLevel(LogConfig.STEAM_LOG_LEVEL)

    # Formatter
    stream_formatter = colorlog.ColoredFormatter(
        LogConfig.STREAM_FORMATTER,
        # log_colors=log_colors_config)
    )
    file_formatter = colorlog.ColoredFormatter(
        # '[%(asctime)s][line: %(lineno)d] ==> %(message)s')
        LogConfig.FILE_FORMATTER,
        # log_colors=log_colors_config)
    )
    # setFormatter
    file.setFormatter(file_formatter)
    stream.setFormatter(stream_formatter)

    # addHandler
    logger.addHandler(file)
    logger.addHandler(stream)

    logger.info('creating {}'.format(final_log_file))
    return logger


# log = log_creater("log")
# print(log.handlers)


if __name__ == "__main__":
    log = log_creater("log")
    log.info("aaa")
    log.debug("---测试开始----")
    log.info("操作步骤")
    log.warning("----测试结束----")
    log.error("----测试错误----")
    log.critical("____________________")
